package com.leaves.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.net.InetAddress;
import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * @Author: LEAVES
 * @Version 1.0
 * @Date: 2022年04月01日 13时28分24秒
 * @Description: 启动
 */
@Component
public class SaTokenDemoStartup implements CommandLineRunner {

    @Value("${spring.application.name}")
    private String applicationName;

    @Value("${server.port}")
    private String port;

    @Value("${server.servlet.context-path}")
    private String path;

    @Value("${spring.profiles.active}")
    private String active;

    @Override
    public void run(String... args) throws Exception {
        String ip = InetAddress.getLocalHost().getHostAddress();
        String str = "\n------------- " + applicationName + " (" + active + ") 启动成功 --by " + getNow() + " -------------\n" +
                "\t- Local:   http://localhost:" + port + path + "\n" +
                "\t- Local2:  http://127.0.0.1:" + port + path + "";
        System.out.println(str);
        //
        try {
            System.out.println("\t- Network: http://" + ip + ":" + port + path + "\n");
        } catch (Exception e) {
            System.out.println("\t- Network: 未能成功获取\n");
            System.err.println("异常：" + e.getMessage());
        }
    }

    /**
     * 返回系统当前时间的YYYY-MM-dd hh:mm:ss 字符串格式
     */
    private static String getNow() {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
    }
}
